import $mConfig from '@/config/index.config.js';
const requestToken = (params) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: $mConfig.identityUrl + 'connect/token',
			method: 'POST',
			data: params,
			header: {
				'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
			},
			success: (res) => {
				if (res.statusCode == 200) {
					resolve(res.data.access_token)
				} else {
					let msg = ''
					msg = params.policy_name == 'password'?'用户名或密码错误，请您重新输入':'短信验证码错误'
					uni.showToast({
						title: msg,
						icon: 'none'
					})
					reject(new Error(res.erroMessage || '请求失败'))
				}
			},
			fail: (err) => {
				uni.showToast({
					title: '网络错误',
					icon: 'none'
				})
				reject(new Error(res.erroMessage || '网络错误'))
			}
		})
	})
}
const changeMobile = (params) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: $mConfig.identityUrl + 'api/passport/changePhoneNumber',
			method: 'POST',
			data: params,
			header: {
				'content-type': 'application/json;charset=UTF-8'
			},
			success: (res) => {
				if (res.data.code == 200) {
					resolve(res.data)
				} else {
					uni.showToast({
						title: res.data.erroMessage,
						icon: 'none'
					})
				}
			},
			fail: (err) => {
				uni.showToast({
					title: '网络错误',
					icon: 'none'
				})
			}
		})
	})
}
const changePass = (params) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: $mConfig.identityUrl + 'api/passport/SetPassword',
			method: 'POST',
			data: params,
			header: {
				'content-type': 'application/json;charset=UTF-8'
			},
			success: (res) => {
				if (res.data.code == 200) {
					resolve(res.data)
				} else {
					uni.showToast({
						title: res.data.erroMessage,
						icon: 'none'
					})
				}
			},
			fail: (err) => {
				uni.showToast({
					title: '网络错误',
					icon: 'none'
				})
			}
		})
	})
}
const sendSms = (params) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: $mConfig.identityUrl + 'api/Passport/CallSendSmsCode',
			method: 'POST',
			data: params,
			header: {
				'content-type': 'application/json;charset=UTF-8'
			},
			success: (res) => {
				resolve(res.data)
			},
			fail: (err) => {
				reject(err)
				uni.showToast({
					title: '网络错误',
					icon: 'none'
				})
			}
		})
	})
}
export {
	requestToken,
	changeMobile,
	changePass,
	sendSms
}